//
//  ViewController.m
//  3DTouch
//
//  Created by yao zhaofeng on 2018/4/16.
//  Copyright © 2018年 YaoZhaofeng. All rights reserved.
//

#import "ViewController.h"
#import "DataUtil.h"
#import "NSArray+FileData.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textField1.text = @"jj斗地主";
    _textField2.text = @"信鸽";
    [self testNSURLParam];
    [self setIcons:NO];
}
- (IBAction)btnClick:(id)sender {
}

- (void)setIcons:(BOOL)isReverted {
    UIImage *image1 = [UIImage imageNamed:@"icon1"];
    UIImage *image2 = [UIImage imageNamed:@"icon2"];
    NSString *value = _textField1.text;
    value = value && value.length > 0 ? value : @"默认1";
    NSDictionary *data1 = @{@"title":value, @"imgData":UIImagePNGRepresentation(image1), @"gameid": @"1001", @"productid":@"3000"};
    value = _textField2.text;
    value = value && value.length > 0 ? value : @"默认2";
    NSDictionary *data2 =@{@"title":value, @"imgData":UIImagePNGRepresentation(image2), @"gameid": @"2001", @"productid":@"4000"};
    NSArray *dataArr = nil;
    if (isReverted) {
        dataArr = @[data2, data1];
    } else {
        dataArr = @[data1, data2];
    }
    NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.xy.3DTouch"];
    containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/iconArr"];
    [dataArr saveToFileURL:containerURL];
}

- (void)testNSURLParam {
    NSURL *url = [NSURL URLWithString:@"3DTouch://hello?gameid=1001&productID=3000"];
    
    NSLog(@"Host: %@", [url host]);
    
    NSLog(@"Port: %@", [url port]);
    
    NSLog(@"User: %@", [url user]);
    
    NSLog(@"Password: %@", [url password]);
    
    NSLog(@"Path: %@", [url path]);
    
    NSLog(@"Fragment: %@", [url fragment]);
    
    NSLog(@"Parameter string: %@", [url parameterString]);
    
    NSLog(@"Query: %@", [url query]);
    
    NSLog(@"Relative path: %@", [url relativePath]);
    
    NSLog(@"Scheme: %@", [url scheme]);
    
    NSLog(@"Path components as array: %@", [url pathComponents]);
    
    //使用NSURLComponents拆解query参数
    NSURLComponents *urlComponent = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
    NSArray *queryItems = urlComponent.queryItems;
    NSLog(@"gameid == %@, productid = %@", [DataUtil valueForKey:@"gameid" fromQueryItems:queryItems], [DataUtil valueForKey:@"productID" fromQueryItems:queryItems]);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)isSupport3DTouch {
    BOOL isSupport = NO;
    if (@available(iOS 9.0, *)) {
        if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
            isSupport = YES;
        }
    }
    
    return isSupport;
}

- (IBAction)buttonClick:(id)sender{
//    if (![self isSupport3DTouch]) {
//        return;
//    }
    NSInteger tag = ((UIButton *)sender).tag;
    switch (tag) {
        case 100:
        {
            //2个`
            if (@available(iOS 9.0, *)) {
                UIApplicationShortcutIcon *searchShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
                UIApplicationShortcutItem *searchShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.xy.search" localizedTitle:@"Search" localizedSubtitle:@"Search Subtitle" icon:searchShortcutIcon userInfo:nil];
                
                UIApplicationShortcutIcon *playShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay];
                UIApplicationShortcutItem *playShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.xy.play" localizedTitle:@"Play" localizedSubtitle:@"Play Subtitle" icon:playShortcutIcon userInfo:nil];
                
                [UIApplication sharedApplication].shortcutItems = @[searchShortcutItem, playShortcutItem];
            } else {
                // Fallback on earlier versions
            }
            
        }
            break;
        case 101:
        {
            //4个
            if (@available(iOS 9.0, *)) {
                UIApplicationShortcutIcon *searchShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
                UIApplicationShortcutItem *searchShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.xy.search" localizedTitle:@"Search" localizedSubtitle:@"Search Subtitle" icon:searchShortcutIcon userInfo:nil];
                
                UIApplicationShortcutIcon *playShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay];
                UIApplicationShortcutItem *playShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.xy.play" localizedTitle:@"Play" localizedSubtitle:@"Play Subtitle" icon:playShortcutIcon userInfo:nil];
                
                UIApplicationShortcutIcon *qrShortcutIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_QR"];
                UIApplicationShortcutItem *qrShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.xy.qr" localizedTitle:@"QR" localizedSubtitle:@"QR Subtitle" icon:qrShortcutIcon userInfo:nil];
                
                UIApplicationShortcutIcon *customIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@""];
                UIApplicationShortcutIcon *payShortcutIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_pay"];
                UIApplicationShortcutItem *payShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.xy.pay" localizedTitle:@"Pay" localizedSubtitle:@"Pay Subtitle" icon:payShortcutIcon userInfo:nil];
                
                [UIApplication sharedApplication].shortcutItems = @[searchShortcutItem, playShortcutItem, qrShortcutItem, payShortcutItem];
            } else {
                // Fallback on earlier versions
            }
        }
            break;
        case 300:
        {
            //设置button1
            NSString *value = _textField1.text;
            value = value && value.length > 0 ? value : @"默认1";
            [DataUtil saveDataByNSUserDefaults:value forKey:@"Widget"];
        }
            break;
        case 301:
        {
            //设置button2
            NSString *value = _textField2.text;
            value = value && value.length > 0 ? value : @"默认2";
            [DataUtil saveDataByNSFileManager:value ForPath:@"Library/Caches/test"];
        }
            break;
        case 400:
        {
            NSArray *dataArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"face" ofType:@"plist"]];
            NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"face" ofType:@"plist"]];
            NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.xy.3DTouch"];
            containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/array"];
            NSError *error = nil;
            [dataArr saveToFileURL:containerURL];
        }
            break;
        case 401:
        {
            [self setIcons:YES];
        }
            break;
        default:
            break;
    }
}


@end
